package com.leosoft.heatmonitor.service.transactional;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.leosoft.heatmonitor.dao.MeterDAO;
import com.leosoft.heatmonitor.dao.UserDAO;
import com.leosoft.heatmonitor.model.User;

@Component("meterService")
public class MeterService {
	@Autowired
	private MeterDAO meterDAO;
	@Autowired
	private UserDAO userDAO;

	public Map<String, Object> list(int startIndex, int length, List<String[]> wherePart, List<String[]> odrerPart,
			String username) {
		User user = userDAO.getUserByName(username);
		if (!user.getRoles().isEmpty()) {
			if (user.getRoles().get(0).getRole().equals("PM")) {
				return meterDAO.list(startIndex, length, wherePart, odrerPart, user.getId());
			} else {
				return meterDAO.list(startIndex, length, wherePart, odrerPart, "");
			}
		}

		return null;
	}

	public boolean create(String deviceKey, String addr1, String name) {
		if (meterDAO.notExist(deviceKey, addr1, name)) {
			meterDAO.create(deviceKey, addr1, name);
			return true;
		}
		return false;
	}

	public void update(String id, String deviceKey, String addr1, String name) {
		meterDAO.update(id, deviceKey, addr1, name);

	}

	public void delete(String id) {
		meterDAO.delete(id);
	}

}
